<?php

error_reporting(0);
require '../../../../../framework/bootstrap.inc.php';
require '../../../../../addons/mx_shop/defines.php';
require '../../../../../addons/mx_shop/core/inc/functions.php';
require '../../../../../addons/mx_shop/core/inc/plugin_model.php';
global $_W, $_GPC;

ignore_user_abort(); //ºöÂÔ¹Ø±Õä¯ÀÀÆ÷
set_time_limit(0); //ÓÀÔ¶Ö´ÐÐ

$sets = pdo_fetchall('select uniacid from ' . tablename('mx_shop_sysset'));
foreach ($sets as $set) {

    $_W['uniacid'] = $set['uniacid'];
    if (empty($_W['uniacid'])) {
        continue;
    }

    $trade = m('common')->getSysset('trade', $_W['uniacid']);
    $days = intval($trade['receive']);

    $p = p('commission');
    $pcoupon = com('coupon');

    $orders = pdo_fetchall("select id,couponid,openid,isparent,sendtime,price,merchid,isverify,addressid,isvirtualsend,virtual,dispatchtype from " . tablename('mx_shop_order') . " where uniacid={$_W['uniacid']} and status=2 ", array(), 'id');

    if (!empty($orders)) {


        foreach ($orders as $orderid => $order) {
            $result = goodsReceive($order, $days);
            if (!$result) {
                //²»×Ô¶¯ÊÕ»õ
                continue;
            }

            pdo_query("update " . tablename('mx_shop_order') . ' set status=3,finishtime=' . time() . ' where id='. $orderid );

            //¶àÉÌ»§¸¸¶©µ¥Ìø¹ý
            if ($order['isparent'] == 1) {
                continue;
            }
            //会员升级
            m('member')->upgradeLevel($order['openid'], $orderid);
            //余额赠送
            m('order')->setGiveBalance($orderid, 1);
            //模板消息
            m('notice')->sendOrderMessage($orderid);
            //商品全返
            m('order')->fullback($orderid);
            //处理积分
            m('order')->setStocksAndCredits($orderid, 1);


            //ÓÅ»ÝÈ¯·µÀû
            if ($pcoupon) {
                //·¢ËÍÔùËÍÓÅ»ÝÈ¯
                com('coupon')->sendcouponsbytask($item['id']); //¶©µ¥Ö§¸¶

                if (!empty($order['couponid'])) {
                    $pcoupon->backConsumeCoupon($order['id']); //×Ô¶¯ÊÕ»õ
                }
            }
            //·ÖÏú¼ì²â
            if ($p) {
                $p->checkOrderFinish($orderid);
            }
            //³é½±
            if(p('lottery') && $order['merchid'] == 0){
                //type 1:Ïû·Ñ 2:Ç©µ½ 3:ÈÎÎñ 4:ÆäËû
                $res = p('lottery')->getLottery($order['openid'],1,array('money'=>$order['price'],'paytype'=>2));
                if($res){
                    p('lottery')->getLotteryList($order['openid'],array('lottery_id'=>$res));
                }
            }
        }
    }
}

function goodsReceive($order, $sysday=0){
    $days = array();
    if (checkFetchOrder($order)){
        //½ûÖ¹×ÔÌá¶©µ¥×Ô¶¯ÊÕ»õ
        return false;
    }

    $isonlyverifygoods = m('order')->checkisonlyverifygoods($order['id']);
    //´¿»áÔ±¿¨ÉÌÆ·¶©µ¥²»×Ô¶¯ÊÕ»õ
    if($isonlyverifygoods)
    {
        return false;
    }

    if ($order['merchid'] == 0) {
        $goods = pdo_fetchall("select og.goodsid, g.autoreceive from".tablename("mx_shop_order_goods") ." og left join ".tablename("mx_shop_goods")." g on g.id=og.goodsid where og.orderid=".$order['id']);

        foreach ($goods as $i=>$g){
            $days[] = $g['autoreceive'];
        }

        $day = max($days);
    } else {
        $day = 0;
    }

    if($day<0){
        return false;
    }
    elseif($day==0){
        if($sysday<=0){
            return false;
        }
        $day = $sysday;
    }

    $daytimes = 86400 * $day;

    if($order['sendtime']+$daytimes<=time()){
        return true;
    }

    return false;
}

//¼ì²éÊÇ·ñÊÇ×ÔÌá¶©µ¥
function checkFetchOrder($order)
{
    //Èç¹û²»ÊÇºËÏú¡¢µØÖ·Îª¿Õ¡¢²»ÊÇÐéÄâ¡¢dispathtype²»Îª¿Õ,ÄÇÃ´¾ÍÊÇ×ÔÌáÉÌÆ·
    if ($order['isverify'] != 1 && empty($item['addressid']) && empty($item['isvirtualsend']) && empty($item['virtual']) && $item['dispatchtype']){
        //ÊÇ×ÔÌáµ¥
        return true;
    }else{
        //²»ÊÇ×ÔÌáµ¥
        return false;
    }
}


